package com.wcsy.myblog.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wcsy.myblog.entity.Comment;
import com.wcsy.myblog.entity.JsonResult;
import com.wcsy.myblog.entity.User;
import com.wcsy.myblog.service.ICommentService;
import com.wcsy.myblog.service.IUserService;
import com.wcsy.myblog.service.serviceImpl.NumServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.jar.JarEntry;

/**
 * 评论信息
 */
@RestController
@RequestMapping("/comment")
@CrossOrigin
@Api(description = "评论信息")
@ApiModel
public class CommentController {
    @Autowired
    private ICommentService iCommentService;
    @Autowired
    HttpSession session;
    @Autowired
    private IUserService iUserService;
    @Autowired
    NumServiceImpl numService;
    /**
     * @apiNote 查询用户的总评论
     * */
    @ApiOperation("查询用户的总评论")
    @GetMapping("/list")
    public JsonResult list(){
        User user = (User) session.getAttribute("user");
        try {
            List<Comment> comments = iCommentService.listComment(user.getUserName());
            return new JsonResult(200,"查询成功",comments);
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(500,"查询失败",null);
        }
    }

    /**
     * 我发表的评论
     * @return
     */
    @ApiOperation("我发表的评论")
    @GetMapping("/MyComment")
    public JsonResult myComment(){
        User user = (User) session.getAttribute("user");
        //我发表的评论
        try {
            List<Comment> comments = iCommentService.MyListComment((int)user.getUserId());
            return new JsonResult(200,"查询成功",comments);
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(500,"查询失败","");
        }
    }

    /**
     * @apiNote 我的文章评论
     * @return
     */
   @ApiOperation("我的文章评论")
   @GetMapping("thisComment")
   public JsonResult thisComment(){
       User user = (User) session.getAttribute("user");
       try {
           List<Comment> comments = iCommentService.thisListComment((int)user.getUserId());
           return new JsonResult(200,"查询成功",comments);
       } catch (Exception e) {
           e.printStackTrace();
           return new JsonResult(500,"查询失败","");
       }
   }

    /**
     * @apiNote 添加评论，显示评论
     * @param comment 用户评论的内容
     * @return
     */
    @ApiOperation("添加评论，显示评论")
    @GetMapping("/insertComment")
    public JsonResult insertComment(Comment comment){
        Date startTime = new Date(System.currentTimeMillis());//获取当前的时间
        String format = new SimpleDateFormat("YYYY年MM月dd日HH:mm").format(startTime);
        comment.setCommentTime(format);
        String getfilter = numService.getfilter(comment.getCommentContent());
        comment.setCommentContent(getfilter);
        boolean b = iCommentService.insertComment(comment);
        if (b){
            List<Comment> commentList = iCommentService.selectOneComment((int) comment.getArticleId());
            return new JsonResult(200,"评论成功",commentList);
        }
        return new JsonResult(500,"评论失败","");
    }
    /**
     * 删除评论
     */
    @ApiOperation("删除评论")
    @GetMapping("/deleteComment")
    public JsonResult deleteComment(int commentId){
        try {
            iCommentService.deleteComment(commentId);
            return new JsonResult(200,"删除成功","");
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(500,"删除失败","");
        }
    }
}
